<template>
  <div>
    <div class="search">
      <div class="clickReturn" @click="clickReturn">&lt;</div>
      <div class="input">

        <input class="inp" ref="inp" type="text" :value="inputValue"   @keyup.enter="inputSearch">
      </div>
      <div class="searchsss" @click="inputSearch">搜索</div>
    </div>
    <div class="false-content">
      <!-- 搜索数据 -->
      <div class="cctt" v-show="showHiddenSearch">
        <div class="item-list" v-for="(item,index) in ReferralSearchData" :key="index"  @click="clickEnden($event)">{{item.name}}</div>
    </div>
    <!-- 商品数据 -->
    <div class="product-box" v-show="(searchTotalData.length > 0 ? true : false)">

      <div class="box-item" v-for="(item,index) in searchTotalData" :key="index" @click="productDetails(item.itemId)">
        <div class="item-img">
          <img class="auto-img" :src="item.img" alt="">
        </div>
        <div class="item-content">
          <div class="content-title ovf-text">{{item.title}}</div>
          <div class="content-fu ">{{item.slogan}}</div>
          <div class="item-price">
            <span class="price-span1">&yen;{{(item.price / 100)}}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="footer" v-show="NoData">
      <van-empty
        class="custom-image"
        image="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg95.699pic.com%2Fphoto%2F40129%2F8276.gif_wh860.gif&refer=http%3A%2F%2Fimg95.699pic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1672903689&t=789611c586ded7bfdb23034023d1335c"
        description="蒽蒽蒽，宝宝努力了，但是宝宝找不到数据/(ㄒoㄒ)/~~"
      />
    </div>

  </div>
  </div>
</template>

<script>
import SearchData from "../../public/json/SearchData.json"
//搜索接口
import { getSearchPageData } from "../api/introduce";

export default {
  name: "ShouSuo",
  data() {
    return {
      SearchData,
      //推举搜索数据
      ReferralSearchData:[],
      //搜索框文本
      inputValue:"摄像机",
      //搜索出来的总数据
      searchTotalData:'',
      //显示隐藏搜索数据页面
      showHiddenSearch:true,
      //没有数据的时候显示
      NoData:false,
      //分类传过来的文本值
   

    }
  },


  created(){
    this.currentText = this.$route.params.currentText
    // console.log("this.$route.params.currentText=>",this.$route.params.currentText);
    if(this.currentText === ""){
      return;
    }else{
      getSearchPageData(this.currentText).then((data)=>{
        console.log(data,'asdfhjgasgdi');
      console.log("data.data==>",data.data);
      this.searchTotalData = data.data.list
      this.showSearch();

      // console.log("this.showHiddenSearch=>",this.showHiddenSearch);
      console.log("this.searchTotalData=>",this.searchTotalData);

     if(this.showHiddenSearch == false && this.searchTotalData == 0){
      this.NoData = !this.NoData
     }

      // console.log("this.searchTotalData=>",this.searchTotalData);

    }).catch(err=>{
      console.log("搜索页面数据报错！！！==>",err);
    })

    }

    this.ReferralSearchData = SearchData.SearchData
    // console.log("SearchData=>",this.ReferralSearchData);
  },
  methods:{
    //跳转到详情页
    productDetails(itemID) {
      // console.log("itemID=>", itemID);
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        loadingType: 'spinner',
      })
      setTimeout(() => {
        this.$router.push({ name: "Details", params: { itemID } });
      }, 1000);
    },

    //输入搜索
    inputSearch(){
      let inpVal = this.$refs.inp.value;
      console.log("inpVal=>",inpVal);
       //拿到搜索数据
      getSearchPageData(inpVal).then((data)=>{
      console.log("data.data==>",data.data);
      this.searchTotalData = data.data.list
      this.showSearch();

      console.log("this.showHiddenSearch=>",this.showHiddenSearch);
      console.log("this.searchTotalData=>",this.searchTotalData);

     if(this.showHiddenSearch == false && this.searchTotalData == 0){
      this.NoData = !this.NoData
     }

      console.log("this.searchTotalData=>",this.searchTotalData);

    }).catch(err=>{
      console.log("搜索页面数据报错！！！==>",err);
    })

    },

    //点击搜索 
    clickEnden(event){
      // 拿到搜索点击数据
      let e = event.currentTarget

      this.inputValue = e.innerText

      //拿到搜索数据
      getSearchPageData(this.inputValue).then((data)=>{
      console.log("data.data==>",data.data);
      this.searchTotalData = data.data.list
      this.showSearch();

      console.log("this.searchTotalData=>",this.searchTotalData);

    }).catch(err=>{
      console.log("搜索页面数据报错！！！==>",err);
    })
    },

    //显示搜索出来的数据的页面
    showSearch(){
      let searchLength = this.searchTotalData.length;
      if(searchLength > 0){
        console.log("aaasdf dfdsfa");
        this.showHiddenSearch = false
      }
    },
    // 返回上一级
    clickReturn(){
      this.$router.back();
    }
  }
};
</script>

<style lang="scss" scoped>
.product-box{
  display: flex;
  flex-wrap: wrap;
  .box-item{
    width: calc(50% - 10px + 5px);
    margin-right: 10px;
    margin-bottom: 10px;
    background-color: white;
    padding: 10px;
    box-sizing: border-box;
    &:nth-child(2n){
      margin-right: 0;
    }

    .item-content{
      font-size: 16px;
      .item-price{
        color: red;
        font-weight: bold;
        margin-top: 5px;
        text-align: center;
        .price-span2{
          font-size: 12px;
          color: #999;
          margin-left: 5px;
        }
      }
      .content-title{
        font-weight: bold;
        // text-align: center;
      }
      .content-fu{
      color: #999;
      font-size: 14px;
      margin-top: 5px;
    }
      
    }
  }
}
.input{
  margin-top: 10px;
}
.inp{
  height: 30px;
  width: 275px;
  font-size: 16px;
  border-radius: 5px;
  background-color: white;
  border: none;
}
.searchsss{
  line-height: 50px;
  padding-left: 10px;
  font-size: 16px;
}
.clickReturn{
  font-size: 25px;
  line-height: 50px;
  margin-right: 20px;
  font-family: "宋体";
  font-weight: bold;
}
.search{
  display: flex;
  padding: 0 10px;
  width: 100%;
  box-sizing: border-box;
  height: 50px;
  background-color: #f0f0f0;
  border-bottom: 1px solid rgb(174, 174, 174);
}
.item-list{
    background-color: white;
    height: 25px;
    padding: 5px 20px;
    line-height: 25px;
    font-size: 16px;
    border-radius: 15px;
    margin-right: 10px;
    margin-top: 20px;
    color: #999;
}

.cctt{
  display: flex;
    flex-wrap: wrap;
}
.false-content{
  position: fixed;
    background-color: #eeeeee;
    padding: 10px;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    box-sizing: border-box;
    overflow-y: auto;
    padding-bottom: 50px;

}
</style>